package com.fulihui.wgj.dal.mybatis.repository;

import com.fulihui.wgj.dal.mybatis.domain.Wx48NewsLog;
import com.fulihui.wgj.dal.mybatis.mapper.Wx48NewsLogMapper;
import com.fulihui.wgj.facade.request.wx48.Wx48NewsLogPageQueryRequest;
import com.fulihui.wgj.facade.request.wx48.Wx48NewsLogQueryRequest;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Component
public class Wx48NewsLogRepository extends BaseRepository<Wx48NewsLog> {

    @Resource
    Wx48NewsLogMapper wx48NewsLogMapper;

    /**
     * 更新48小时发送记录-累加
     *
     * @param wx48NewsLog
     * @return
     */
    public int updateNewsLogAccumulate(Wx48NewsLog wx48NewsLog) {
        return wx48NewsLogMapper.updateNewsLogAccumulate(wx48NewsLog);
    }

    /**
     * 不分页查询
     *
     * @param request
     * @return
     */
    public List<Wx48NewsLog> queryByRequest(Wx48NewsLogQueryRequest request) {
        return this.queryByExample(buildExample(request));
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public PageInfo<Wx48NewsLog> pageQueryByRequest(Wx48NewsLogPageQueryRequest request) {
        return this.queryPageListByExample(buildExample(request), request.getPage(), request.getRows());
    }

    /**
     * 组装条件
     *
     * @param request
     * @return
     */
    Example buildExample(Wx48NewsLogQueryRequest request) {
        Example example = new Example(Wx48NewsLog.class);
        Example.Criteria criteria = example.createCriteria();
        String appId = request.getAppId();
        String status = request.getStatus();
        String name = request.getName();
        Date startTime = request.getStartTime();
        Date endTime = request.getEndTime();
        if (StringUtils.isNotBlank(appId)) {
            criteria.andEqualTo("appId", appId);
        }
        if (StringUtils.isNotBlank(status)) {
            criteria.andEqualTo("status", status);
        }
        if (StringUtils.isNotBlank(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (Objects.nonNull(startTime)) {
            criteria.andGreaterThanOrEqualTo("gmtModified", startTime);
        }
        if (Objects.nonNull(endTime)) {
            criteria.andLessThanOrEqualTo("gmtModified", endTime);
        }
        example.orderBy("gmtModified").desc();
        return example;
    }

    /**
     * 组装条件
     *
     * @param request
     * @return
     */
    Example buildExample(Wx48NewsLogPageQueryRequest request) {
        Example example = new Example(Wx48NewsLog.class);
        Example.Criteria criteria = example.createCriteria();
        String appId = request.getAppId();
        String status = request.getStatus();
        String name = request.getName();
        Date startTime = request.getStartTime();
        Date endTime = request.getEndTime();
        if (StringUtils.isNotBlank(appId)) {
            criteria.andEqualTo("appId", appId);
        }
        if (StringUtils.isNotBlank(status)) {
            criteria.andEqualTo("status", status);
        }
        if (StringUtils.isNotBlank(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (Objects.nonNull(startTime)) {
            criteria.andGreaterThanOrEqualTo("gmtModified", startTime);
        }
        if (Objects.nonNull(endTime)) {
            criteria.andLessThanOrEqualTo("gmtModified", endTime);
        }
        example.orderBy("gmtModified").desc();
        return example;
    }
}